from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import AuthenticationForm
from .models import UserProfile, Subject, LEVEL_CHOICES

class RegisterForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)
    confirm_password = forms.CharField(widget=forms.PasswordInput)
    chinese_level = forms.ChoiceField(choices=LEVEL_CHOICES, label="汉语等级")
    subject_code = forms.CharField(label="学科代码", required=False)
    subject_name = forms.CharField(label="学科名称", required=False)

    class Meta:
        model = User
        fields = ['username', 'email', 'password']

    def clean(self):
        cleaned_data = super().clean()
        password = cleaned_data.get("password")
        confirm_password = cleaned_data.get("confirm_password")

        if password and confirm_password and password != confirm_password:
            raise forms.ValidationError("密码不匹配")
        return cleaned_data
